
//
//  SampuxEquipmentInstallViewController.m
//  Sampux
//
//  Created by rzm on 16/7/6.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SPEquipmentInstallViewController.h"
#import "SampuxEquipmentInstallView.h"
#import "SampuxInstallPermissionsVC.h"
#import "SampuxSetAliasView.h"
#import "SampuxInstallTimeVC.h"
#import "SampuxUnBindView.h"
#import "SampuxLogOutViewController.h"
#import "SPDeviceMessage.h"
#import "SPDeviceConfigModel.h"
#import "SampuxEquipDetailViewController.h"

@interface SPEquipmentInstallViewController ()<SampuxEquipmentSelectedRowDelegate,SendAlisaStrDelegate>
{
    NSIndexPath *_indexPath;
}
@property (nonatomic, strong) SampuxSetAliasView *setAliasView;
@property (nonatomic, strong) SampuxEquipmentInstallView *installView ;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) SampuxUnBindView *unbindView;
@property (nonatomic, strong)  SPDeviceConfigModel *configModel;
@property(nonatomic,assign) BOOL needRefresh;
@property (nonatomic, strong) UIButton *leftBtn;
@property(nonatomic,assign) BOOL isConnectOrNot;

@end

@implementation SPEquipmentInstallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:YES];
    self.isConnectOrNot = YES;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestConfig:) name:NOTIFICATION_RESPONSE_DEVICE_CONFIG object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setParameterSuccess) name:@"setParameterSuccess" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setParameterSuccess) name:@"setOwnChangeSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unbindViewRemove) name:@"UnbindViewWithTouch" object:nil];

    [self createSettingView];

}

- (void)setParameterSuccess
{
    self.needRefresh = YES;
}

- (void)createSettingView
{
    _installView = [[SampuxEquipmentInstallView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [self.view addSubview:_installView];
    _installView.deviceDelegate = self;
}

- (SampuxSetAliasView *)setAliasView
{
    if (!_setAliasView ) {
        _setAliasView = [[SampuxSetAliasView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _setAliasView.model = self.model;
        _setAliasView.aliasType = SampuxSetAliasView_Device;
        _setAliasView.aliasView.textFiled.text = self.configModel.alias;
    }
    return _setAliasView;
}

- (SampuxUnBindView *)unbindView
{
    if (_unbindView == nil) {
        _unbindView = [[SampuxUnBindView alloc]initWithFrame:self.view.bounds];
        _unbindView.model = self.model;
        __weak typeof(SPEquipmentInstallViewController *) wSelf = self;
        _unbindView.cancelBlock = ^()
        {
            [wSelf cancelDeleteEquipMent];
            if (_model.isPossessor == YES) {
                UITableViewCell *cell = [wSelf.installView.installTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]];
                cell.backgroundColor = THWhiteAlplaColor(0.1);
            } else {
                UITableViewCell *cell = [wSelf.installView.installTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]];
                cell.backgroundColor = THWhiteAlplaColor(0.1);
            }
        };
    
        _unbindView.unBindBlock = ^
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                SampuxCustomNavController *nav = (SampuxCustomNavController *)wSelf.navigationController;
                nav.rightBtn.hidden = YES;
                [wSelf.navigationController popToRootViewControllerAnimated:YES];
            });
        };
    }
    return _unbindView;
}

- (void)setModel:(SampuxListModel *)model
{
    _model = model;
    if (_model != nil) {
        if (_model.isPossessor == YES) {
            self.unbindView.unBindType = SampuxDelete_Type_UnbindDevice_isPosser;
        } else {
            self.unbindView.unBindType = SampuxDelete_Type_UnbindDevice_isNotPosser;
        }
        _installView.model = _model;
    }
}

- (void)seletedRow:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    if (self.configModel.isPossessor == 1) {
        if (indexPath.row == 1)
        {
            AppDelegate *delegate = [UIApplication sharedApplication].delegate;
            delegate.window.backgroundColor = THSystemBlackColor;
            [delegate.window addSubview:self.setAliasView];
            self.setAliasView.alisaDelegate = self;
            self.setAliasView.aliasView.textFiled.text = self.configModel.alias;

        } else if (indexPath.row == 4) {
            SampuxInstallTimeVC *timeVC = [[SampuxInstallTimeVC alloc]init];
            if (self.configModel != nil) {
                timeVC.listModel = self.model;
//                timeVC.configModel = self.configModel;
            }
            [self.navigationController pushViewController:timeVC animated:YES];

        } else if (indexPath.row == 5) {
            SampuxInstallPermissionsVC *permissionsVC = [[SampuxInstallPermissionsVC alloc]init];
            if (self.configModel != nil) {
                permissionsVC.model = self.configModel;
                self.rightBtn.hidden = YES;
                [self.navigationController pushViewController:permissionsVC animated:YES];
            }
          
        } else if (indexPath.row == 6)
        {
            SampuxLogOutViewController *logoutVC  = [[SampuxLogOutViewController alloc]init];
            if (self.configModel != nil) {
                //                logoutVC.listModel = self.model;
                
                logoutVC.model = self.configModel;
                [self.navigationController pushViewController:logoutVC animated:YES];
            }
        } else if (indexPath.row == 7)
        {
            AppDelegate *delegate = [UIApplication sharedApplication].delegate;
            delegate.window.backgroundColor = THSystemBlackColor;
            [delegate.window addSubview:self.unbindView];
        }
    } else
    {
        if (indexPath.row == 4)
        {
            SampuxInstallTimeVC *timeVC = [[SampuxInstallTimeVC alloc]init];
            if (self.configModel != nil) {
                timeVC.listModel = self.model;
//                timeVC.configModel = self.configModel;
                [self.navigationController pushViewController:timeVC animated:YES];
            }
        } else if (indexPath.row == 5)
        {
            SampuxLogOutViewController *logoutVC  = [[SampuxLogOutViewController alloc]init];
            if (self.configModel != nil) {
                logoutVC.model = self.configModel;
                [self.navigationController pushViewController:logoutVC animated:YES];
            }
        } else if (indexPath.row == 6)
        {
            AppDelegate *delegate = [UIApplication sharedApplication].delegate;
            delegate.window.backgroundColor = THSystemBlackColor;
            [delegate.window addSubview:self.unbindView];
        }
    }
}

- (void)sendAlisaStr:(NSString *)aliasStr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        SPDeviceConfigModel *model = _installView.configModel;
        //    SPDeviceConfigModel *model = _installView.dataArr[0];
        model.alias = aliasStr;
        //    _installView.dataArr =  _installView.dataArr;
        _installView.configModel = model;

    });
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    UITableViewCell *cell = [_installView.installTableView cellForRowAtIndexPath:_indexPath];
    cell.backgroundColor = THWhiteAlplaColor(0.1);
    SampuxCustomNavController *nav = (SampuxCustomNavController *)self.navigationController;
    nav.button.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    SampuxCustomNavController *nav = (SampuxCustomNavController *)self.navigationController;
    nav.titleStr = @"设置";
    nav.rightBtn.hidden = NO;
//    nav.button.hidden = YES;
    nav.img.image = imageNamed(@"nav");

    if (self.needRefresh == YES) {
        [_installView.header beginRefreshing];
    }
//    [self createBarButtonItem];

}

- (void)cancelDeleteEquipMent
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.unbindView removeFromSuperview];
    });
}

- (void)requestConfig:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"requestConfig == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    if(responseCode == 0x00){
        NSLog(@"deviceAccount  == %@,isDataSyn == %@,ownAccount == %@，isPossessor == %@，alias == %@",[result objectForKey:@"deviceAccount"],[result objectForKey:@"isDataSyn"],[result objectForKey:@"ownAccount"],[result objectForKey:@"isPossessor"],[result objectForKey:@"alias"]);
        [self getData:result];
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"获取设备配置失败"];
//        [MBProgressHUD showError:@"获取设备配置失败"];
    }
}

- (void)getData:(NSDictionary *)result
{
    [_installView.dataArr removeAllObjects];

    _configModel = [[SPDeviceConfigModel alloc]init];
    _configModel.deviceAccount = result[@"deviceAccount"];
    _configModel.isDataSyn = [result[@"isDataSyn"] integerValue];
    _configModel.ownAccount = result[@"ownAccount"];
    _configModel.isPossessor = [result[@"isPossessor"] integerValue];
    _configModel.alias = STRING_FORMAT(result[@"alias"]) ;
    [_installView.dataArr addObject:_configModel];
    _installView.configModel = _configModel;
//    _configModel.limitedArr = self.model.limitArr;
    _installView.dataArr = _installView.dataArr;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (_configModel.isPossessor == YES) {
            self.unbindView.unBindType = SampuxDelete_Type_UnbindDevice_isPosser;
        } else
        {
            self.unbindView.unBindType = SampuxDelete_Type_UnbindDevice_isNotPosser;
        }
    });
}

- (void)unbindViewRemove
{
    NSIndexPath *index;
    if (self.model.isPossessor) {
        index = [NSIndexPath indexPathForRow:7 inSection:0];
    } else
    {
        index = [NSIndexPath indexPathForRow:6 inSection:0];
    }
    UITableViewCell *cell = [_installView.installTableView cellForRowAtIndexPath:index];
    cell.backgroundColor = THWhiteAlplaColor(0.1);
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
